﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XuanXiang.Infrastructure.Attr;
using XuanXiang.Infrastructure.Dto.Request;
using XuanXiang.Infrastructure.Dto.Response;
using XuanXiang.Infrastructure.Dto.Response.Log;
using XuanXiang.Infrastructure.EFCore.MySql;
using XuanXiang.Infrastructure.IOC;
using XuanXiang.Service.Interface;

namespace XuanXiang.Service.Instance
{
    [Inject_]
    public class Log_Service : Base_Service, I_Log_Service
    {
        private readonly Log_IOC _log_IOC;
        private readonly Login_IOC _login_IOC;

        public Log_Service(Log_IOC log_IOC,Login_IOC login_IOC)
        {
            _log_IOC = log_IOC;
            _login_IOC = login_IOC;
        }

        /// <summary>
        /// 新增错误日志
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Api_Result_Dto AddErrorLog(SysError log)
        {
            _log_IOC._sysErrorLog_EFCore.Add(log);
            var result = _log_IOC._sysErrorLog_EFCore.SaveChanges();
            return Get_Result(result);
        }

        /// <summary>
        /// 新增日志
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Api_Result_Dto AddLog(Infrastructure.EFCore.MySql.SysActionLog log)
        {
            _log_IOC._sysActionLog_EFCore.Add(log);
            var reuslt = _log_IOC._sysActionLog_EFCore.SaveChanges();
            return Get_Result(reuslt);
        }



        /// <summary>
        /// 获取所有的操作日志
        /// </summary>
        /// <returns></returns>
        public async Task<Api_Result_Dto> Get_Active_Logs(Fenye_Dto dto)
        {
            var u_ids=await _login_IOC._sysUser_EFCore.GetAll(
                d=>
                ((string.IsNullOrEmpty(dto.account) || d.Uaccount!.Contains(dto.account)))
                &&
                ((string.IsNullOrEmpty(dto.name) || d.Uname!.Contains(dto.name)))
                )
                .Select(d=>d.Uid).ToListAsync();

            var data = await _log_IOC._sysActionLog_EFCore.GetAll(out int total, dto.page, dto.limit,
                false, o => o.AlCreateTime,d=>u_ids.Contains(d.AlUId!))
                .Select(d=>new Log_Action_Dto() { 
                id=d.AlId,
                account="",
                name="",
                action=d.AlAction,
                group=d.AlGroup,
                methods=d.AlMethod,
                parameters=d.AlParams,
                time=d.AlCreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                uid=d.AlUId,
                url=d.AlUrl
                })
                .ToListAsync();
            //获取所有用户id
            var users=await _login_IOC._sysUser_EFCore.GetAll().ToListAsync();
            data.ForEach(d => {
                var u = users.Where(c => c.Uid==d.uid).SingleOrDefault();
                d.account = u.Uaccount;
                d.name = u.Uname;
            });
            return Get_Result(0, "ok", new
            {
                data = data,
                total = total
            });
        }

        /// <summary>
        /// 获取所有的错误日志
        /// </summary>
        /// <param name="fenye_Dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Result_Dto> Get_Error_Log(Fenye_Dto dto)
        {
            var data = await _log_IOC._sysErrorLog_EFCore
                .GetAll(out int total, dto.page, dto.limit, false, o => o.ECreateTime)
                .Select(d => new Log_Error_Dto(){ 
                id=d.EId,
                message=d.EMessage,
                methods=d.EMethod,
                parameter=d.EParams,
                time=d.ECreateTime!.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                url=d.EUrl
                }).ToListAsync();

            return Get_Result(0, "ok", new { 
            data=data,
            total = total
            });
        }

        /// <summary>
        /// 获取所有的登录日志
        /// </summary>
        /// <param name="fenye_Dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Result_Dto> Get_Login_Log(Fenye_Dto dto)
        {
            //获取对应account/name的用户id
            var u_ids = await _login_IOC._sysUser_EFCore.GetAll(d=>
            (string.IsNullOrEmpty(dto.account) || (d.Uaccount.Contains(dto.account)))
            &&
            (string.IsNullOrEmpty(dto.name) || (d.Uname.Contains(dto.name)))
            ).Select(d=>d.Uid).ToListAsync();

            var data = await _log_IOC._sysLoginLog_EFCore
                .GetAll(out int total, dto.page, dto.limit, false, o => o.LlcreateTime,
                d=>u_ids.Contains(d.Lluid!)
                )
                .Select(d => new Log_Login_Dto()
                {
                    id=d.Llid,
                    time=d.LlcreateTime!.Value.ToString("yyyy-MM-dd HH:mm:sss"),
                    name="",
                    account="",
                    uid=d.Lluid!
                }).ToListAsync();
            //获取所有的用户
            var users = await _login_IOC._sysUser_EFCore.GetAll().ToListAsync();

            data.ForEach( d => {
                var user =  users.Where(c => c.Uid==d.uid).SingleOrDefault();
                d.account = user.Uaccount;
                d.name = user.Uname!;
            });

            return Get_Result(0, "ok", new { 
            data=data,
            total=total
            });
            

        }
    }
}
